﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class EditProduct : BasePage
    {
        private IBrandBLL brandBLL = IocContainer.Instance.Resolve<IBrandBLL>();
        private IProductBLL productBLL = IocContainer.Instance.Resolve<IProductBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ddlBrand.DataSource = brandBLL.GetBrand("", null);
                this.ddlBrand.DataBind();
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int productId = 0;
                    int.TryParse(Request.QueryString["ProductId"], out productId);
                    if (productId > 0)
                    {
                        Skyworth.CRM.Service.Entity.Product product = productBLL.GetProductById(productId);
                        this.ddlBrand.SelectedValue = product.BrandId.ToString();
                        this.ddlProuctType.SelectedValue = product.ProductTypeId.ToString();
                        this.txtProductTypeId.Value = product.ProductTypeId.ToString();
                        this.txtProductName.Text = product.ProductName;
                        this.txtProductCode.Text = product.ProductCode;
                        this.txtProductModel.Text = product.ProductModel;
                        this.txtProductSize.Text = product.ProductSize;
                        this.txtProductUnit.Text = product.ProductUnit;
                        this.txtUnitVolume.Text = product.UnitVolume;
                        this.txtSource.Text = product.Source;
                        this.txtPackingSize.Text = product.PackingSize;
                        this.txtDescription.Text = product.Desctipion;
                        this.IsDetail.Checked= product.IsDetail;
                        this.txtPackStandard1.Text = product.PackStandard1;
                        this.txtPackStandard2.Text = product.PackStandard2;
                        this.txtPackStandard3.Text = product.PackStandard3;
                        string imagesPath = "../ProductImages/Images/";
                        string thumbnailPath = "../ProductImages/Thumbnail/";
                        string noProductImage = thumbnailPath + "No.jpg";
                        this.Image1.NavigateUrl = string.IsNullOrEmpty(product.Image1) ? "#" : imagesPath + product.Image1;
                        this.Image2.NavigateUrl = string.IsNullOrEmpty(product.Image2) ? "#" : imagesPath + product.Image2;
                        this.Image3.NavigateUrl = string.IsNullOrEmpty(product.Image3) ? "#" : imagesPath + product.Image3;
                        this.Image4.NavigateUrl = string.IsNullOrEmpty(product.Image4) ? "#" : imagesPath + product.Image4;
                        this.txtImage1.Value = string.IsNullOrEmpty(product.Image1) ? "#" : product.Image1;
                        this.txtImage2.Value = string.IsNullOrEmpty(product.Image2) ? "#" : product.Image2;
                        this.txtImage3.Value = string.IsNullOrEmpty(product.Image3) ? "#" : product.Image3;
                        this.txtImage4.Value = string.IsNullOrEmpty(product.Image4) ? "#" : product.Image4;

                        this.thumbnail1.ImageUrl = string.IsNullOrEmpty(product.Thumbnail1) ? noProductImage : thumbnailPath + product.Thumbnail1;
                        this.thumbnail2.ImageUrl = string.IsNullOrEmpty(product.Thumbnail2) ? noProductImage : thumbnailPath + product.Thumbnail2;
                        this.thumbnail3.ImageUrl = string.IsNullOrEmpty(product.Thumbnail3) ? noProductImage : thumbnailPath + product.Thumbnail3;
                        this.thumbnail4.ImageUrl = string.IsNullOrEmpty(product.Thumbnail4) ? noProductImage : thumbnailPath + product.Thumbnail4;
                        this.thumbnail1.AlternateText =
                        this.thumbnail2.AlternateText =
                        this.thumbnail3.AlternateText = this.thumbnail4.AlternateText = product.ProductName;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                Skyworth.CRM.Service.Entity.Product product = new CRM.Service.Entity.Product();
                product.BrandId = Convert.ToInt32(this.ddlBrand.SelectedValue);
                product.ProductName = this.txtProductName.Text;
                product.ProductCode = this.txtProductCode.Text;
                product.ProductModel = this.txtProductModel.Text;
                product.ProductSize = this.txtProductSize.Text;
                product.ProductUnit = this.txtProductUnit.Text;
                product.UnitVolume = this.txtUnitVolume.Text;
                product.Source = this.txtSource.Text;
                product.ProductTypeId = Convert.ToInt32(this.txtProductTypeId.Value);
                product.PackingSize = this.txtPackingSize.Text;
                product.IsDetail = this.IsDetail.Checked;
                product.Desctipion = this.txtDescription.Text;
                product.PackStandard1 = this.txtPackStandard1.Text.Trim();
                product.PackStandard2 = this.txtPackStandard2.Text.Trim();
                product.PackStandard3 = this.txtPackStandard3.Text.Trim();

                product.Image1 = this.txtImage1.Value.Trim('#');
                product.Image2 = this.txtImage2.Value.Trim('#'); 
                product.Image3 = this.txtImage3.Value.Trim('#'); 
                product.Image4 = this.txtImage4.Value.Trim('#'); 
                product.Thumbnail1 = this.txtImage1.Value.Trim('#'); 
                product.Thumbnail2 = this.txtImage2.Value.Trim('#'); 
                product.Thumbnail3 = this.txtImage3.Value.Trim('#'); 
                product.Thumbnail4 = this.txtImage4.Value.Trim('#'); 
                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    productBLL.Add(product);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int productId = 0;
                    int.TryParse(Request.QueryString["ProductId"], out productId);
                    product.ProductId= productId;

                    productBLL.Update(product);
                    isOK = true;
                }
               if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：" + exception.Message);
            }
        }
    }
}
